package com.mjt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Supplier;
import com.mjt.admin.mapper.SupplierMapper;
import com.mjt.admin.query.SupplierQuery;
import com.mjt.admin.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjt.admin.utils.AssertUtil;
import com.mjt.admin.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 供应商表 服务实现类
 * </p>
 *
 * @author 莫某人
 * @since 2021-11-01
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {

    @Override
    public RespDataBean<Supplier> supplierList(SupplierQuery supplierQuery) {
        IPage<Supplier> page = new Page<Supplier>(supplierQuery.getPage(),supplierQuery.getLimit());
        QueryWrapper<Supplier> wrapper = new QueryWrapper<Supplier>().eq("is_del", 0);

        if (!StringUtil.isEmpty(supplierQuery.getSupplierName())){
            wrapper.like("name",supplierQuery.getSupplierName());
        }

        page = this.baseMapper.selectPage(page, wrapper);


        return new RespDataBean<Supplier>(0,"查询成功",page.getRecords(),page.getTotal());


    }

    @Override
    public void saveSupplier(Supplier supplier) {
        Supplier supplierTemp = this.getByName(supplier.getName());
        //判断供应商名称不能为空
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getName()),"应商名称不能为空!");
        AssertUtil.isTrue(supplierTemp!=null,"应商名称已存在!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getContact()),"联系人不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getNumber()),"联系电话不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getAddress()),"联系地址不能为空!");
        AssertUtil.isTrue(!this.save(supplier),"添加失败");
    }

    @Override
    public void updateSupplier(Supplier supplier) {

        Supplier supplierTemp = this.getByName(supplier.getName());


        //判断供应商名称不能为空
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getName()),"应商名称不能为空!");
        AssertUtil.isTrue(supplierTemp!=null,"应商名称已存在!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getContact()),"联系人不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getNumber()),"联系电话不能为空!");
        AssertUtil.isTrue(StringUtil.isEmpty(supplier.getAddress()),"联系地址不能为空!");
        AssertUtil.isTrue(!this.updateById(supplier),"更新成功");
    }

    @Override
    public Supplier getByName(String name) {
        return this.baseMapper.selectOne(new QueryWrapper<Supplier>().eq("name",name));
    }

    @Override
    public void deleteSupplier(Integer[] ids) {
        List<Supplier> suppliers = new ArrayList<>();
        for (Integer id : ids) {
            suppliers.add(new Supplier(id,1));
        }

        AssertUtil.isTrue(!this.updateBatchById(suppliers),"删除供应商信息成功！");
    }
}
